package com.zt.rongda.workbill.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.workbill.model.WorkBill;
import com.zt.rongda.workbill.service.WorkBillService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.util.DateUtil;

@Controller
@RequestMapping(value = "/workbill")
public class WorkbillController {

    @Autowired
    private WorkBillService workBillService;

    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(workBillService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        workBillService.add(getWorkBill(reqBean),reqBean);
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        workBillService.delete(reqBean.getInfo("workbillId"));
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/send")
    @ResponseBody
    public JSONObject send(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        workBillService.send(reqBean.getInfo("workbillId"),reqBean);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/item/delete")
    @ResponseBody
    public JSONObject deleteItem(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        workBillService.deleteItem(reqBean.getInfo("workbillItemId"));
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/items")
    @ResponseBody
    public JSONObject getWorkbillItems(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(workBillService.getWorkbillItems(reqBean.getInfo("workbillId")));
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private WorkBill getWorkBill(ReqBean reqBean) {
        WorkBill workBill = new WorkBill();
        workBill.setWorkbillId(reqBean.getInfo("workbillId"));
        workBill.setOrderId(reqBean.getInfo("orderId"));
        workBill.setWorkbillType(reqBean.getInfo("workbillType"));
        workBill.setSender(reqBean.getInfo("sender"));
        workBill.setSendTime(DateUtil.parse(reqBean.getInfo("sendTime")));
        workBill.setSendRemark(reqBean.getInfo("sendRemark"));
        workBill.setStatus(reqBean.getInfo("status"));
        workBill.setOperator(reqBean.getUserId());
        workBill.setRemark(reqBean.getInfo("remark"));
        return workBill;
    }

}